package com.wisdytech.common.websocket.handler;

import com.alibaba.fastjson.JSON;
import com.wisdytech.linkdcs.configuration.service.ITtEqConfigurationDataService;
import com.wisdytech.linkdcs.equipment.dto.EquipAlarmDataDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

@Service
public class RedisReceiver {
	
	private static Logger logger =LoggerFactory.getLogger(RedisReceiver.class);
	
	@Autowired
	private ITtEqConfigurationDataService dataService;
	
	
	/**
	 * 处理告警信息
	 * @param message
	 */
	public void receiveMessage(String message) {
		
		logger.info("收到设备告警信息 !"+message);
		List<EquipAlarmDataDto> alarmListMsg =new ArrayList<>();
		EquipAlarmDataDto alarmMsg=null;
		try {
			//告警信息
			alarmMsg =
					JSON.parseObject(message,EquipAlarmDataDto.class);
			alarmListMsg.add(alarmMsg);
		} catch (Exception e) {
			logger.error("转换告警信息失败",e);
			return;
		}
		
		
		//告警信息推送
		//获取缓存的session
		Set<Entry<String, WebSocketSession>> entrys =
				WebSocketPushHandler.userSessionLists.entrySet();
		
		for(Entry<String, WebSocketSession> entry : entrys) {
			
			WebSocketSession session =entry.getValue();
			dataService.pushAlarmEquipmentData(alarmListMsg, session);
			
		}
	}
}
